const { getPage } = require("../../methods");
const { PARAMS_ERROR } = require("../../../utils/errorCode");

exports.main = (req, res) => {
    console.log(req.body);
    // 获取数据
    const {
        pageNo = 1,
        pageSize = 20,
        isActive
    } = req.body
    console.log(pageNo, pageSize);
    let sql = `select productId,productName,productPrice,productInventory,cover,isActive,products.createTime,categoryName,
    products.categoryId
    from products,categories
               where isDelete = '0' and products.categoryId = categories.categoryId order by createTime desc limit ?,?;
    select count(*) count
    from products
    where isDelete = '0'; `;
    if (isActive) {
        if (!['0', '1'].includes(isActive)) {
            return res.err(PARAMS_ERROR, "是否上架参数错误，应为 0 或 1");
        } else if (isActive === '1') {
            sql = `select productId,productName,productPrice,productInventory,cover,isActive,products.createTime,categoryName,
           products.categoryId
                   from products,categories
                   where isDelete = 0
                     and isActive = 1 and products.categoryId = categories.categoryId order by createTime desc limit ?,?;
            select count(*) count
            from products
            where isDelete = 0 and isActive = 1; `;
        } else if (isActive === '0') {
            sql = `select productId,productName,productPrice,productInventory,cover,isActive,products.createTime,categoryName,
            products.categoryId
            from products,categories
                   where isDelete = 0
                     and isActive = 0  and products.categoryId = categories.categoryId order by createTime desc limit ?,?;
            select count(*) count
            from products
            where isDelete = 0 and isActive = 0; `;
        }
    }

    return getPage(res, sql, pageNo, pageSize);
}